package JUC_Advanced.chapter1_CompletableFuture.api;

import JUC_Advanced.ThreadPool;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: GuSongLiao
 * @Date: 2024/3/7 10:53
 * @Description: api deno
 */

/**
 * thenRun thenRun(Runnablerunnable)
 * 任务A执行完执行B，并且B不需要A的结果(重新开启一个线程执行B)
 *
 * thenAccept thenAccept(Consumer action)
 * 任务A执行完执行B，B需要A的结果，但是任务B无返回值
 *
 * thenApply thenApply(Function fn)
 * 任务A执行完执行B，B需要A的结果，同时任务B有返回值
 */
public class CompletableFutureApi_then_ {

    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            return "买鱼";
        }, ThreadPool.THREAD_POOL).thenApply(fish -> {
            return fish + " 腌制";
        }).handle((fish, e )-> { // 报错仍会执行
            int i = 10 / 0;
            return fish + " 煎";
        }).handle((fish, e )-> {
            return fish + " 摆盘";
        }).whenComplete((t, u) -> {
            System.out.println("---" + t);
        }).exceptionally(e -> {
            return "出错了";
        });


        System.out.println("主线程忙别的去了");
    }
}
